/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import modelo.Curso;
import modelo.Docente;
import modelo.Turma;
import modelo.TurmaPK;

/**
 *
 * @author Caps
 */
@Stateless
public class TurmaFacade extends AbstractFacade<Turma> {
    @PersistenceContext(unitName = "SagrauPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TurmaFacade() {
        super(Turma.class);
    }

    public List<Docente> carregarDocentes(Turma turma) {
        TurmaPK turmaPK;
        turmaPK = turma.getTurmaPK();
        TypedQuery<Turma> q = getEntityManager().createQuery(
                "select t from Turma t where t.turmaPK.prdltvPrdletivo =:arg1 AND t.turmaPK.crsCodigo =:arg2 AND t.turmaPK.dscCodigo =:arg3 AND t.turmaPK.trmcbcSerie =:arg4 AND t.turmaPK.trmcbcSequencia =:arg5 AND t.turmaPK.trmCodigo =:arg6", Turma.class);
                q.setParameter("arg1", turmaPK.getPrdltvPrdletivo());
                q.setParameter("arg2", turmaPK.getCrsCodigo());
                q.setParameter("arg3", turmaPK.getDscCodigo());
                q.setParameter("arg4", turmaPK.getTrmcbcSerie());
                q.setParameter("arg5", turmaPK.getTrmcbcSequencia());
                q.setParameter("arg6", turmaPK.getTrmCodigo());

        q.setMaxResults(1);

        List<Turma> lista = q.getResultList();
        if (lista == null) {
            return null;
        }
        
        List<Docente> list = new ArrayList<Docente>();
        for (int i = 0; i < lista.size(); i++) {
            List<Docente> listDocente = lista.get(i).getDocenteList();
            for (int j = 0; j < listDocente.size(); j++) {
                list.add(listDocente.get(j)); 
            }
        }

        return list;
    }
        
    public List<Curso> carregarCurso(Turma turma) {

        TurmaPK turmaPK;
        turmaPK = turma.getTurmaPK();
        TypedQuery<Turma> q = getEntityManager().createQuery(
                "select t from Turma t where t.turmaPK.prdltvPrdletivo =:arg1 AND t.turmaPK.crsCodigo =:arg2 AND t.turmaPK.dscCodigo =:arg3 AND t.turmaPK.trmcbcSerie =:arg4 AND t.turmaPK.trmcbcSequencia =:arg5 AND t.turmaPK.trmCodigo =:arg6", Turma.class);
        q.setParameter("arg1", turmaPK.getPrdltvPrdletivo());
        q.setParameter("arg2", turmaPK.getCrsCodigo());
        q.setParameter("arg3", turmaPK.getDscCodigo());
        q.setParameter("arg4", turmaPK.getTrmcbcSerie());
        q.setParameter("arg5", turmaPK.getTrmcbcSequencia());
        q.setParameter("arg6", turmaPK.getTrmCodigo());

        q.setMaxResults(1);

        List<Turma> lista = q.getResultList();
        if (lista == null) {
            return null;
        }

        List<Curso> cursos = new ArrayList<Curso>();
        
        for (int i = 0; i < lista.size(); i++) {
            cursos.add(lista.get(i).getCurso());
        }


        return cursos;
    }
}
